<?php
if (!defined('IN_MEDIA')) die("Hacking attempt");
# ALBUM INFO
if(is_numeric($id)) {
	$sql 								= "SELECT * FROM ".$tb_prefix."album WHERE album_id = $id";
	$query 								= $mysql->query($sql);
		if($mysql->num_rows($query)) {
				//////// INFO ALBUMS /////////
				$mysql->query("UPDATE ".$tb_prefix."album SET album_viewed = album_viewed + 1 WHERE album_id = '$id'");
				$row 					= $mysql->fetch_array($query);
				$album_ID				= $row['album_id'];
				$album_NAME 			= $row['album_name'];
				
				$singer_ID				= $row['album_singer'];
				$singer_NAME 			= m_get_data('SINGER',$singer_ID);
				
				$album_INFO	 			= ($row['album_info'])?m_text_tidy($row['album_info']):'Chưa có';
				$album_IMG				= m_get_img('Album',$row['album_img']);
				
				$singer_URL  	 		= url_link($singer_ID,$singer_NAME,'Singer');
				$playAlbum_URL			= url_link($album_ID,$album_NAME,'PlayAlbum');
				######################
				##### SONGS IN ALBUM #####
				######################
					#############
					## PHAN TRANG ##
					#############
					// CHECK TOTAL SONGS
					$num 				= $mysql->query("SELECT * FROM ".$tb_prefix."data WHERE m_album = $id");
					$total 				= $mysql->num_rows($num);
					if (!$pg) $pg 		= 1;
					$m_per_page 		= m_get_config("media_per_page");
					$sql_song 			= "SELECT * FROM ".$tb_prefix."data WHERE m_album = $album_ID LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
					$query_song 		= $mysql->query($sql_song);
					if($mysql->num_rows($query_song)) {
						while($row_song = $mysql->fetch_array($query_song)) {
							switch ($row_song['m_type']) {
								case 1 : $media_type = 'music'; break;
								case 2 : $media_type = 'flash'; break;
								case 3 : $media_type = 'movie'; break;
							}
				$song_TYPE 		 		= "<img src='/templates/".$_SESSION['current_tpl']."/img/media/type/$media_type.gif'>";
				$song_LYRIC 	 		= ($row_song['m_lyric'])?"<img src='/templates/".$_SESSION['current_tpl']."/img/media/ok.gif'>":'';

				$song_ID 		 		= $row_song['m_id'];
				$song_TITLE 	 		= $row_song['m_title'];
				$user_ID = $row_song['m_poster'];
				$cat_ID =  $row_song['m_cat'];
				$user_NAME =  m_get_data('USER',$user_ID);
				$cat_NAME =  m_get_data('CAT',$cat_ID);
				$cat_URL    = url_link($cat_ID,$cat_NAME,'Cat');
				$song_VIEWED	 		= $row_song['m_viewed'];
				$song_DOWNLOADED 		= $row_song['m_downloaded'];
				$sql_album = $mysql->fetch_array($mysql->query("SELECT COUNT(m_album) as al FROM ".$tb_prefix."data WHERE m_album = $album_ID"));
				extract($sql_album);
				$count_albums = $al;
				$singer_ID		 		= $row_song['m_singer'];
				$singer_NAME 			= m_get_data('SINGER',$singer_ID);
				
				$song_URL  		 		= url_link($song_ID,$song_TITLE,'nhac-san');
				$singer_URL  	 		= url_link($singer_ID,$singer_NAME,'Singer');

				$link			 		= (m_get_config('url_type') == 0)?$mainURL."/index.php?go=album&id=".$id."&pg=":$mainURL."/Album/".$album_ID."/".$album_NAME."/Page/";
				$view_pages 			= m_viewpages($total,$m_per_page,$pg,$link);
				$song_TITLE1 = $song_TITLE;
				if($user_NAME == '')
					{
						$user_NAME = 'DJ Other';
					}
				$addp = 'addToPlaylist('.$song_ID.');return false;';	
				if (!$isLoggedIn){
					$addp = 'alert("Yêu cầu đăng nhập")';
				}
				$user_URL	= url_link($user_ID,$user_NAME,'User');
				$m_rate = $row_song['m_rating'];
				$m_rate_total = $row_song['m_rating_total'];
				if ($m_rate =='0') $current_star = 0;

				else $rater_rating = $m_rate / $m_rate_total;
				if ($rater_rating <= 0  ) $star = 0;
				if ($rater_rating >= 0.5) $star = 1;
				if ($rater_rating >= 1) $star = 2;
				if ($rater_rating >= 1.5) $star = 3;
				if ($rater_rating >= 2) $star = 4;
				if ($rater_rating >= 2.5) $star = 5;
				if ($rater_rating >= 3) $star = 6;
				if ($rater_rating >= 3.5) $star = 7;
				if ($rater_rating >= 4) $star = 8;
				if ($rater_rating >= 4.5) $star = 9;
				if ($rater_rating >= 5) $star = 10;
				$song_HTML		 	   .= "<li><p class='right'><span class='rate s_".$star."'><span><span><span><span><span></span></span></span></span></span></span><span class='add_pl'><a href='#' onclick='$addp' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL' title='$song_TITLE'>$song_TITLE1</a></p><p class='user'>Đăng bởi: <a href='$user_URL'>$user_NAME</a> | <a href='$cat_URL'>$cat_NAME</a> | Lượt nghe: $song_VIEWED</p></li>";
				}
			}
					if(!$mysql->num_rows($query_song)) {
						$song_HTML 		= "<li>Chưa có bài hát trong Album này</li>";
					}		
	include("templates/".$_SESSION['current_tpl']."/album_info.html");
	}
}
if($id == '0') { 
					#############
					## PHAN TRANG ###
					#############
					// CHECK TOTAL SONGS
					$num 				= $mysql->query("SELECT * FROM ".$tb_prefix."data WHERE m_album = $id");
					$total 				= $mysql->num_rows($num);
					if (!$pg) $pg 		= 1;
					$m_per_page 		= m_get_config("media_per_page");
					$sql_song 			= "SELECT * FROM ".$tb_prefix."data WHERE m_album = $id LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
					$query_song 		= $mysql->query($sql_song);
					if($mysql->num_rows($query_song)) {
						while($row_song = $mysql->fetch_array($query_song)) {
							switch ($row_song['m_type']) {
								case 1 : $media_type = 'music'; break;
								case 2 : $media_type = 'flash'; break;
								case 3 : $media_type = 'movie'; break;
							}
				$song_TYPE 		 		= "<img src='/templates/".$_SESSION['current_tpl']."/img/media/type/$media_type.gif'>";
				$song_LYRIC 		    = ($row_song['m_lyric'])?"<img src='/templates/".$_SESSION['current_tpl']."/img/media/ok.gif'>":'';

				$song_ID 		 		= $row_song['m_id'];
				$song_TITLE 	 		= $row_song['m_title'];
				
				$singer_ID				= $row_song['m_singer'];
				$singer_NAME    		=  m_get_data('SINGER',$row_song['m_singer']);
				
				$song_VIEWED	 		= $row_song['m_viewed'];
				$song_DOWNLOADED 		= $row_song['m_downloaded'];
				$sql_album = $mysql->fetch_array($mysql->query("SELECT COUNT(m_album) as al FROM ".$tb_prefix."data WHERE m_album = $album_ID"));
				extract($sql_album);
				$count_albums = $al;
				$song_URL  		 		= url_link($song_ID,$song_TITLE,'Play/Song');
				$singer_URL  	 		= url_link($singer_ID,$singer_NAME,'Singer');
				
				$link			 		= (m_get_config('url_type') == 0)?"/index.php?go=album&id=0&pg=":"/Album/0/Dang-Cap-Nhat/Page/";
				$view_pages = m_viewpages($total,$m_per_page,$pg,$link);
				
				$song_HTML			   .= "<li><p class='right'<span class='time'>".$song_VIEWED."</span><span class='add_pl'><a href='#' onclick='addToPlaylist($song_ID);return false;' title='Add to Playlist'></a></span></p><p class='title'><a href='$song_URL'>$song_TITLE</a></p><p class='user'>thanhvien</p></li>";
				}
			}
					if(!$mysql->num_rows($query_song)) {
						$song_HTML	    = "<li>Chưa có bài hát trong Album này</li>";
					}			
	include("templates/".$_SESSION['current_tpl']."/album_info.html");
}

// List Album
if($show == 'list') {
$sql_order = "ORDER BY album_id DESC";
	#############
	## PHAN TRANG ##
	#############
	// CHECK TOTAL SONGS
	$num 								= $mysql->query("SELECT * FROM ".$tb_prefix."album");
	$total 								= $mysql->num_rows($num);
	if (!$pg) $pg = 1;
	$m_per_page 						= m_get_config("media_per_page");
	// QUERY SQL
	$sql 								= "SELECT * FROM ".$tb_prefix."album $sql_order LIMIT ".(($pg-1)*$m_per_page).",".$m_per_page;
	$query 								= $mysql->query($sql);
	if($mysql->num_rows($query)) {
		$n= "";
		while($row = $mysql->fetch_array($query)) {
			$n++;
			$class = "";
			if(fmod($n,3) == 1) $class= " class=\"first\"";
			$album_ID 		 			= $row['album_id'];
			$album_NAME 	 			= $row['album_name'];
			
			$singer_ID					= $row['album_singer'];
			$singer_NAME    			=  m_get_data('SINGER',$singer_ID);
			if($singer_NAME)
			{
				$singer_URL	= "<a href=\"".url_link($singer_ID,$singer_NAME,'Singer')."\">".m_get_data('SINGER',$singer_ID)."</a>";
			
			}
			else $singer_URL	= "&nbsp;";
			$album_VIEWED				= $row['album_viewed'];
			$album_IMG					= m_get_img('Album',$row['album_img']);
			
			$album_URL					= url_link($album_ID,$album_NAME,'Album');
			
			
			$title 						= "Danh sách Album";		
			$link		 				= (m_get_config('url_type') == 0)?$mainURL."/index.php?go=album&show=list&pg=":$mainURL."/List/Album/Page/";		
			$view_pages 				= m_viewpages($total,$m_per_page,$pg,$link);
			
			$album_HTML		  		   .= "<li".$class."><p class=\"thumb\"><a href=\"$album_URL\"><img src=\"$album_IMG\"</span></a></p>
			<div class=\"text\">
			<h2 class=\"title\"><a href=\"$album_URL\">$album_NAME</a></h2>
			<p class='user'>$singer_URL</p></div>
							</li>";
		}
	}
include("templates/".$_SESSION['current_tpl']."/list_album.html");
}
if($play) {
	$sql 								= "SELECT * FROM ".$tb_prefix."album WHERE album_id = $play";
	$query 								= $mysql->query($sql);
	$r	 								= $mysql->fetch_array($query);
	$album_ID 							= $r['album_id'];
	$album_NAME							= $r['album_name'];
	
	$album_INFO 						= m_text_tidy($r['album_info']);
	$album_IMG							= m_get_img('Album',$r['album_img']);
	$sql_album = $mysql->fetch_array($mysql->query("SELECT COUNT(m_album) as al FROM ".$tb_prefix."data WHERE m_album = $album_ID"));
	extract($sql_album);
	$count_albums = $al;	
	$singer_ID							= $r['album_singer'];
	$singer_NAME						= m_get_data('SINGER',$singer_ID);
	
	$singer_URL							= url_link($singer_ID,$singer_NAME,'Singer');
	$album_URL							= url_link($album_ID,$album_NAME,'Album');
	
	$url_play 							= $mainURL.'/Album/3/'.$album_ID.'.xml';
	
	include("templates/".$_SESSION['current_tpl']."/play_album.html");
}

	
?>